package com.gxa.controller.parking;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.parking.PRegionDto;
import com.gxa.dto.parking.PRegionSearchDto;
import com.gxa.entity.parking.PRegion;
import com.gxa.entity.parking.Parking;
import com.gxa.service.parking.PRegionService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;


/**
 * <p>
 *  前端控制器
 *
 * </p>
 *区域划分
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "区域划分")
@RestController
@RequestMapping("/p-region")

public class PRegionController {
    @Autowired
    private PRegionService pRegionService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("查询区域")
    @GetMapping("/list")
    public Result selectAll(PRegionSearchDto pRegionSearchDto, Integer pageNum, Integer pageSize){
        PageInfo<PRegionDto> pRegionDtoPageInfo;
        try {
            pRegionDtoPageInfo = pRegionService.queryAll(pRegionSearchDto, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(pRegionDtoPageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增区域接口")
    @PostMapping("/add")
    public Result add(@RequestBody PRegion pRegion){
        try {
            pRegionService.add(pRegion);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("添加失败");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除区域接口")
    @GetMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try {
            pRegionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("删除失败");
        }
        return Result.success();
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询区域接口")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Integer id){
        PRegion pRegion;
        try {
            pRegion = pRegionService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(pRegion);
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑区域接口")
    @PostMapping("/edit")
    public Result edit(@RequestBody PRegion pRegion){
        try {
            pRegionService.update(pRegion);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("编辑失败");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("查询所有停车场的接口")
    @GetMapping("/queryParking")
    public Result queryParking(){
        List<Parking> parkings;
        try {
             parkings = pRegionService.queryParking();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(parkings);
    }

}
